package org.example.controller;

import org.example.domain.dto.AddTagDto;
import org.example.domain.dto.EditTagDto;
import org.example.domain.dto.TagListDto;
import org.example.domain.entity.Tag;
import org.example.domain.result.ResponseResult;
import org.example.domain.vo.TagVo;
import org.example.service.TagService;
import org.example.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/content/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    //----------------------查询标签列表----------------------------
    @GetMapping("/list")
    public ResponseResult list(Integer pageNum, Integer pageSize, TagListDto tagListDto){
        //list是mybatisplus提供的方法
        return tagService.pageTaglist(pageNum,pageSize,tagListDto);
    }

    //-----------------------新增标签----------------------------
    @PostMapping
    public ResponseResult addTag(@RequestBody AddTagDto addTagDto){
        return tagService.addTag(addTagDto);
    }

    //-------------------根据id查询标签------------------------------
    @GetMapping("/{id}")
    public ResponseResult getInfoById(@PathVariable Long id){
        Tag tag = tagService.getById(id);
        return ResponseResult.okResult(tag);
    }


    //-----------------------修改标签-----------------------------------
    @PutMapping
    public ResponseResult editTag(@RequestBody EditTagDto editTagDto){
        Tag tag = BeanCopyUtils.copyBean(editTagDto,Tag.class);
        tagService.updateById(tag);
        return ResponseResult.okResult();
    }



    //-------------------------删除标签--------------------------------
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Long id){
        tagService.removeById(id);
        return ResponseResult.okResult();
    }
    @DeleteMapping
    public ResponseResult remove(@RequestParam(value = "ids")String ids) {
        if (!ids.contains(",")) {
            tagService.removeById(ids);
        } else {
            String[] idArr = ids.split(",");
            for (String id : idArr) {
                tagService.removeById(id);
            }
        }
        return ResponseResult.okResult();
    }

    //---------------------------写博文-查询文章标签的接口---------------------------

    @GetMapping("/listAllTag")
    public ResponseResult listAllTag(){
        List<TagVo> list = tagService.listAllTag();
        return ResponseResult.okResult(list);
    }

}